package com.qxzn.app.page.robot;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.R;
import com.qxzn.app.page.R2;
import com.qxzn.app.page.widget.ToastUtil;
import com.qxzn.app.page.widget.statusbar.StatusBar;
import com.qxzn.app.util.Constant;
import com.qxzn.app.util.SharePreferenceUtil;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AddRobotActivity1 extends BaseActivity {
    public static String ADMIN_NAME = "admin_name";
    public static String ROBOT_NAME = "robot_name";

    @BindView(R2.id.tv_robot_name)
    TextView robotName;
    @BindView(R2.id.tv_admin_name)
    TextView adminName;
    @BindView(R2.id.tv_robot_id)
    TextView robotId;

    @Override
    protected int initLayout() {
        return R.layout.activity_add_robot1;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void setStatusBarColor() {
        StatusBar.setStatusBarLightMode(this, getResources().getColor(R.color.blue_3FC7FF));
    }

    @Override
    protected void iniTitleBar() {
        title_bar.setVisibility(View.GONE);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        getRobotInfo();
    }

    private void getRobotInfo() {
        robotId.setText("ID:" + getIntent().getStringExtra(Constant.PutExtraKey.DEVICE_ID));
        robotName.setText(getIntent().getStringExtra(ROBOT_NAME));
        adminName.setText("管理员:" + getIntent().getStringExtra(ADMIN_NAME));
    }

    public void onClickByPassword(View view) {
        startActivity(new Intent(this, AddRobotActivity2.class)
                .putExtra(Constant.PutExtraKey.DEVICE_ID, getIntent().getStringExtra(Constant.PutExtraKey.DEVICE_ID))
                .putExtra(ROBOT_NAME, getIntent().getStringExtra(ROBOT_NAME)));
    }

    public void onClickByAsk(View view) {
        bingRobot();
    }

    public void onClickBack(View view) {
        finish();
    }

    private void bingRobot() {
        showProgress();
        JSONObject params = new JSONObject();
        params.put("device_id", getIntent().getStringExtra(Constant.PutExtraKey.DEVICE_ID));
        params.put("username", SharePreferenceUtil.getUserName(getApplicationContext()));
        HttpRequest.getService().bingingByAsk(HttpRequest.getDefaultBody(params)).enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                hideProgress();
                BaseBean body = response.body();
                if (body.getCode() == 200)
                    ToastUtil.showImgToast(getApplicationContext(), "已发送", R.mipmap.icon_success);
                else
                    ToastUtil.showTextToast(getApplicationContext(), body.getMsg());
            }

            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
                hideProgress();
                ToastUtil.showTextToast(getApplicationContext(), getString(R.string.network_error));
            }
        });
    }

}
